import GameManager from "../game/GameManager";
import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import { GameEvent } from "../libs/GameEvent";
import Helper from "../libs/HelperMgr";


const {ccclass, property} = cc._decorator;

@ccclass
export default class UIGetTool extends cc.Component {

    @property(cc.Node)
    tool: cc.Node = null;

    private toolNum:number = 1;
    private toolPrice:number = 0;


   

    start () {
        this.initStoshInfo();
        this.refreshStoshInfo();

    }

    async initStoshInfo(){
        let img = this.tool.getChildByName("img").getComponent(cc.Sprite);
        let name = this.tool.getChildByName("name").getComponent(cc.Label);
        if(GameData.instance.shopIsStosh){
            img.spriteFrame = await Helper.loadRes(`image/stosh${GameData.instance.shopIndex}`, cc.SpriteFrame)
            name.string = Constants.StoshInfo[GameData.instance.shopIndex-1].name;
        }else{
            img.spriteFrame = await Helper.loadRes(`image/rod${GameData.instance.shopIndex}`, cc.SpriteFrame)
            name.string = Constants.RodInfo[GameData.instance.shopIndex].name;
        }
    }

    refreshStoshInfo(){
        if(GameData.instance.shopIsStosh){
            this.toolPrice = Constants.StoshInfo[GameData.instance.shopIndex-1].price * this.toolNum;
        }else{
            this.toolPrice = Constants.RodInfo[GameData.instance.shopIndex].price * this.toolNum;
        }
        let stoshnum = this.tool.getChildByName("exchangenum").getComponent(cc.Label);
        let stoshprice = this.tool.getChildByName("pricenum").getComponent(cc.Label);
        stoshnum.string = this.toolNum.toString();
        stoshprice.string = this.toolPrice.toString();
    }

    public onExchangeAdd(){
        if(GameData.instance.shopIsStosh){
            this.toolNum  ++ ;
            this.refreshStoshInfo();
        }
    
    }

    public onExchangeMin(){
        if(GameData.instance.shopIsStosh){
            this.toolNum  -- ;
            if(this.toolNum < 0){
                this.toolNum = 0;
            }
            this.refreshStoshInfo();
        }
       
    }


    public onSure(){
        if(GameData.instance.shopIsStosh){
            if(GameData.instance.playGold  >= this.toolPrice){
                GameData.instance.playGold -= this.toolPrice
                GameData.instance.playStoshs[GameData.instance.shopIndex-1] += this.toolNum;
                GameEvent.emit(Constants.StageEvent.RefreshShop);
                this.onClose();
            }else{
                Helper.showTip("钻石不足");
            }
        }else{
            if(GameData.instance.playGold  >= this.toolPrice){
                GameData.instance.playGold -= this.toolPrice
                GameData.instance.playRods[GameData.instance.shopIndex-1] += this.toolNum;
                GameEvent.emit(Constants.StageEvent.RefreshShop);
                this.onClose();
            }else{
                Helper.showTip("钻石不足");
            }
        }
        
        
        
    }

    public onCancel(){
        this.onClose();
       
    }

    public onClose(){
        if(GameManager.instace){
            GameManager.instace.playerIsStosh = GameData.instance.playStoshs[GameData.instance.playStosh-1] > 0
        }
        GameData.instance.saveData();
        this.node.removeFromParent();
        this.node.destroy();
    }



}
